package cn.com.netedge.iot.protocol.utils;

import com.alibaba.fastjson2.JSON;
import cn.com.netedge.iot.common.constants.MqConstants;
import cn.com.netedge.iot.common.utils.StringUtil;
import cn.com.netedge.iot.model.freeze.FreezeDataModel;
import cn.com.netedge.iot.model.freeze.FreezeEventDataModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 发送存盘报文
 */
public class DataSaveUtil {
    private static Logger logger = LoggerFactory.getLogger(DataSaveUtil.class);

    private static final String TABLE_DATA_E_FRAUDDETECTION = "data_e_frauddetection";
    private static final String TABLE_DATA_C_DAILY =  "data_c_daily";

    public static void save(Object data) {
            String tableName, queueName;
            if (data instanceof FreezeDataModel )  {
                FreezeDataModel freezeDataModel = (FreezeDataModel) data;
                tableName = freezeDataModel.getTableName();
                queueName = getQueueName(tableName);
            } else if(data instanceof FreezeEventDataModel) {
                FreezeEventDataModel dataModel = (FreezeEventDataModel) data;
                tableName = dataModel.getTableName();
                queueName = getQueueName(tableName);
            } else {
                logger.error("data is Illegal! save data={}", JSON.toJSONString(data));
                return;
            }
            if (StringUtil.isEmpty(queueName)) {
                logger.debug("tableName:{} -> queueName is not exist!", tableName);
                return;
            }
            JmsMsgUtil.sendMessage(queueName, JSON.toJSONString(data));
    }

    private static String getQueueName(String tableName) {
        if (TABLE_DATA_E_FRAUDDETECTION.equalsIgnoreCase(tableName)) {
            return MqConstants.QUEUE_DATA_C_EVENT_FRAUDDETECTION_MESSAGE;
        } else if (TABLE_DATA_C_DAILY.equalsIgnoreCase(tableName)) {
            return MqConstants.QUEUE_DATA_C_DAILY_MESSAGE;
        }
        return null;
    }
}
